﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Entity;
using LotteryVote.Data;
using LotteryVote.Utility;
using LotteryVote.Logic.Exceptions;

namespace LotteryVote.Logic.Member
{
    public class MemberStrategy : UserStrategy
    {
        #region params
        private int[] species;
        private int[] groupIds;
        private SystemRole role;
        #endregion

        private MemberPackageDataAccess _daMemberPackage;
        internal MemberPackageDataAccess DaMemberPackage
        {
            get
            {
                if (_daMemberPackage == null)
                    _daMemberPackage = new MemberPackageDataAccess();
                return _daMemberPackage;
            }
            set
            {
                _daMemberPackage = value;
            }
        }

        private CommissionGroupDataAccess _daCommGroup;
        internal CommissionGroupDataAccess DaCommGroup
        {
            get
            {
                if (_daCommGroup == null)
                    _daCommGroup = new CommissionGroupDataAccess();
                return _daCommGroup;
            }
            set
            {
                _daCommGroup = value;
            }
        }

        public override void AddUser(Entity.User user)
        {
            if (user.UserInfo.ShareRate > 1)
                user.UserInfo.ShareRate = user.UserInfo.ShareRate.PercentageToDecimal(2);
            species = this.ParseParam<int[]>(SPECIES);
            groupIds = this.ParseParam<int[]>(GROUPIDS);
            role = this.ParseParam<SystemRole>(ROLE);
            var parent = DaUser.GetUserById(user.ParentId);
            CheckParentRole(parent, role - 1);
            CheckGivenCredit(user, parent);
            user.RoleId = (int)role;
            user.UserInfo.AvailableGivenCredit = user.UserInfo.GivenCredit;
            CheckUserValid(user);
            CheckShareRate(parent, user);
            var groups = DaCommGroup.GetCommissionGroupByGroupId(groupIds);
            if (groups.Count == 0)
                throw new InvalidDataException("groups's count equals 0");
            var betLimitList = DaBetLimit.ListLimitByUser(user.ParentId);
            var gameBetLimitList = DaGameBetLimit.ListLimitByUser(user.ParentId);
            DaUser.ExecuteWithTransaction(() =>
            {
                InsertUser(user);
                UpdateParentGivenCredit(parent, parent.UserInfo.AvailableGivenCredit - user.UserInfo.GivenCredit);
                InitUserCommission(user, species);
                InitUserBetLimit(user, betLimitList);
                InitUserGameBetLimit(user, gameBetLimitList);
                AddMemverPackage(user, groups);
            });
        }

        private void AddMemverPackage(User user, IList<CommissionGroup> groups)
        {
            DaMemberPackage.Tandem(DaUser);
            foreach (var group in groups)
            {
                DaMemberPackage.InsertMemberPackage(user, group);
            }
        }
    }
}
